from mysql_connect.get_data import GetData
from mysql_connect.dependent_data import DependdentData
from run_assert.cookie_token import CookieToken
# from util.send_email import SendEmail
from logs.user_log import UserLog
from run_assert.assert_run import AssertRun
# data = GetData()
cookie_token = CookieToken()
log = UserLog().get_log()
assert_run = AssertRun()
depend = DependdentData()
data_db = GetData()


class RunTest(object):

    def go_on_run(self, case_dict):
        (sql_id, case_id, case_name, url, cookie, token, headers, header_type, method, data_dict,
        pre_case_id, pre_key, pos_data_key, expect, result_key) = data_db.get_data_list(case_dict)
        # log.debug('\n开始运行: | {} | {} |'.format(case_id, case_name))
        # 处理依赖请求，并返回更新后的请求data
        pre_assert = ''; pre_assert02 = ''
        if pre_key and pre_case_id is not None:
            log.debug('case_id "{}"->需要依赖->前置用例"{}"'.format(case_id, pre_case_id))
            request_data_dict, pre_assert, pre_assert02 = depend.get_pre_data(sql_id, pre_case_id, data_dict, pos_data_key)
            log.debug('\n\n已更新依赖数据, case_id切换为"{}"，执行后置接口'.format(case_id))
            flag, msg = depend.run_pre_case_write(sql_id, case_id, pre_key, token, cookie, method, url, request_data_dict, headers, header_type, expect, result_key)
            return flag, msg, pre_assert, pre_assert02
        elif pre_key is not None:
            log.debug('当前case是前置请求，需要写入前置数据；case_id为"{}"')
            flag, msg = depend.run_pre_case_write(sql_id, case_id, pre_key, token, cookie, method, url, data_dict, headers, header_type, expect, result_key)
            return flag, msg, pre_assert, pre_assert02
        elif pre_case_id is not None:
            log.debug('case_id "{}"->需要依赖->前置用例"{}"'.format(case_id,pre_case_id))
            request_data_dict, pre_assert, pre_assert02 = depend.get_pre_data(sql_id, pre_case_id, data_dict, pos_data_key)
            log.debug('\n\n已更新依赖数据, case_id切换为"{}"，执行后置接口'.format(case_id))
            flag, msg = cookie_token.run_assert(sql_id, token, cookie, method, url, request_data_dict, headers, header_type, expect, result_key)
            return flag, msg, pre_assert, pre_assert02
        else:
            flag, msg = cookie_token.run_assert(sql_id, token, cookie, method, url, data_dict, headers, header_type, expect, result_key)
            return flag, msg, pre_assert, pre_assert02


# self.send_main.send_main(pass_count,fail_count)

